/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.oplevka.lahvodroid;

import android.util.Log;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



/**
 *
 * @author Ondra
 */
public class DataHandler extends DefaultHandler { 
 
  // booleans that check whether it's in a specific tag or not 
  private boolean inSection, inArea, inPivo; 
 
  // this holds the data 
  private Data data; 
 
  /** 
   * Returns the data object 
   * 
   * @return 
   */ 
  public Data getData() { 
    return data; 
  } 
 
  /** 
   * This gets called when the xml document is first opened 
   * 
   * @throws SAXException 
   */ 
  @Override 
  public void startDocument() throws SAXException { 
    data = new Data(); 
  } 
 
  /** 
   * Called when it's finished handling the document 
   * 
   * @throws SAXException 
   */ 
  @Override 
  public void endDocument() throws SAXException { 
 
  } 
 
  /** 
   * This gets called at the start of an element. Here we're also setting the booleans to true if it's at that specific tag. (so we 
   * know where we are) 
   * 
   * @param namespaceURI 
   * @param localName 
   * @param qName 
   * @param atts 
   * @throws SAXException 
   */  
  @Override
  public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 
 
    if(localName.equals("section")) { 
      inSection = true;
 
      data.sectionId = atts.getValue("id"); 
    } else if(localName.equals("area")) { 
      inArea = true; 
    }
  } 
 
  /** 
   * Called at the end of the element. Setting the booleans to false, so we know that we've just left that tag. 
   * 
   * @param namespaceURI 
   * @param localName 
   * @param qName 
   * @throws SAXException 
   */ 
  @Override 
  public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 
    Log.v("endElement", localName); 
 
    if(localName.equals("section")) { 
      inSection = false; 
    } else if(localName.equals("area")) { 
      inArea = false; 
    } 
  } 
 
  /** 
   * Calling when we're within an element. Here we're checking to see if there is any content in the tags that we're interested in 
   * and populating it in the Config object. 
   * 
   * @param ch 
   * @param start 
   * @param length 
   */ 
  @Override 
  public void characters(char ch[], int start, int length) { 
    String chars = new String(ch, start, length); 
    chars = chars.trim(); 
 
    if(inSection) { 
      data.section = chars; 
    } else if(inArea) { 
      data.area = chars; 
    } 
  } 
}
